﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Timers;
using System.Windows.Threading;
using PRScreenWithKinect.Model;

namespace PRScreenWithKinect.UI
{
    /// <summary>
    /// Interaction logic for MarqueeText.xaml
    /// </summary>
    public partial class MarqueeText : UserControl
    {
        private const int PERIODS_UPDATE_MIN = 1 * 1000 * 10;
        private const string welcome_word = "Welcome to IT@KMITL";
        private const int _runningtime = 7;
        private const int _holdtime = 5; 
        private Storyboard storyboardMarquee;
        private Timer holdtime;
        private Timer updateTime;
        private List<string> arrContent;
        private int contentIndex = 0;
        private DataFactory datafactory; 
        public MarqueeText()
        {
            InitializeComponent();

            /// Get DataFactoryInstance 
            /// 
            datafactory = DataFactory.Instance;

            /// Simulation prepare content 
            arrContent = new List<string>() ;
            arrContent.Add(welcome_word);

            //// Create Marquee Storyboard 
            DoubleAnimation widthAnimation = new DoubleAnimation();

            widthAnimation.From = canMain.Width;
            widthAnimation.To = Canvas.GetLeft(tbmarquee);
            widthAnimation.Duration = new Duration(TimeSpan.FromSeconds(_runningtime));
            widthAnimation.Completed += new EventHandler(doubleAnimation_Completed);
            
            storyboardMarquee = new Storyboard();
            storyboardMarquee.Children.Add(widthAnimation);
     
            Storyboard.SetTarget(widthAnimation, tbmarquee);
            Storyboard.SetTargetProperty(widthAnimation, new PropertyPath("(Canvas.Left)"));
            
            /// Start Storyboard 
            startMarqueeing();

            /// Create Holdtimer 
            holdtime = new Timer(_holdtime * 1000);
            holdtime.Elapsed += new ElapsedEventHandler(holdtime_Elapsed);

            /// Update Interval
            updateTime = new Timer(PERIODS_UPDATE_MIN);
            updateTime.Enabled = true;
            updateTime.Elapsed += new ElapsedEventHandler(updateTime_Elapsed);
            updateTime.Start();

        }

        void updateTime_Elapsed(object sender, ElapsedEventArgs e)
        {
            /// Request Data from DataFactory
            setContent();
           
        }
        private void setContent()
        {
            arrContent.Clear();
            arrContent.Add(welcome_word);
            if (datafactory.marquee != null)
            {
                foreach (MarqueeTextModel ms in datafactory.marquee)
                {
                    arrContent.Add(ms.content);
                }
            }
        
        }
     private  void startMarqueeing()
        {
      
            tbmarquee.Text = arrContent[contentIndex];
            contentIndex = (contentIndex + 1) % arrContent.Count;
          //  Console.WriteLine("Length content" +contentIndex);

            storyboardMarquee.Seek(TimeSpan.FromSeconds(0));
            storyboardMarquee.Begin();
        }

      private   void doubleAnimation_Completed(object sender, EventArgs e)
        {
           
            holdingMarquee();
        }
      private   void holdingMarquee()
        {
           // Console.WriteLine("end round ");
            holdtime.Start();
       
        }

       private  void holdtime_Elapsed(object sender, ElapsedEventArgs e)
        {
            storyboardMarquee.Dispatcher.Invoke(DispatcherPriority.Normal,
            new Action(delegate(){
                startMarqueeing();
            }));
            ((Timer)sender).Stop();
            
        }
     // Change Bg color state = 0(orange) 1(black) 
       public void changeBackground(int state)
       {
           if (state == 1)
           {
               BackgroundMarquee.Fill = new SolidColorBrush(Colors.Black);
           }
           else {
               BackgroundMarquee.Fill = new SolidColorBrush(Colors.Orange);
           }
       }
     
        
    }
}
